<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="学校名称" prop="schoolId" v-if="oneKey">
        <el-select
          v-model.trim="formData.schoolId"
          clearable
          placeholder="请选择学校"
          filterable
          class="!w-240px"
        >
          <el-option
            v-for="item in schoolList"
            :key="item.id"
            :label="item.schoolName"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <cascadeSelectionStudent ref="selection" :useType="true" v-else />
      <el-form-item label="升学类型" prop="type">
        <el-select v-model="formData.type" placeholder="请选择升学类型" class="!w-240px">
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.CMIS_CONTINUE_STUDY_TYPE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
    </el-form>

    <div style="color: red">* 毕业年级学生转毕业、非毕业年级学生升年级</div>

    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>

<script setup lang="ts">
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
import * as StudentApi from '@/api/cmis/student'
import * as SchoolApi from '@/api/cmis/school'
// const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('升学') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formData = ref({
  schoolId: undefined,
  collegeId: undefined,
  majorId: undefined,
  classesId: undefined,
  type: undefined
})
const formRules = reactive({
  schoolId: [{ required: true, message: '学校不能为空', trigger: 'blur' }],
  type: [{ required: true, message: '升学类型不能为空', trigger: 'change' }]
})
const oneKey = ref(false)
const formRef = ref() // 表单 Ref
const schoolList = ref([]) //学校列表
const selection = ref()
/** 打开弹窗 */
const open = async (flat = false, title = '升学') => {
  oneKey.value = flat
  dialogVisible.value = true
  dialogTitle.value = title
  resetForm()
  getSchoolLists()
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 接收子组件的数据

  if (!oneKey.value) {
    const selectData = { ...selection.value.selectData }
    formData.value = Object.assign(formData.value, selectData)
  }
  // 校验表单
  if (!formRef.value) return
  const valid = await formRef.value.validate()
  if (!valid) return
  // 提交请求
  formLoading.value = true
  try {
    //接口
    const res = await StudentApi.ContinueStudy(formData.value)
    console.log(res)
    message.success('升学成功')
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

// 获取学校列表
const getSchoolLists = async () => {
  const res = await SchoolApi.getSchoolList({})
  schoolList.value = res
}

// // //学校切换
// const schoolChange = (val) => {
//   formData.value.schoolId = val
// }

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    schoolId: undefined,
    collegeId: undefined,
    majorId: undefined,
    classesId: undefined,
    type: undefined
  }
  formRef.value?.resetFields()
}
</script>
